<?php
namespace app\pay\controller;
use think\Input;
use think\Controller;
class View extends Common{
    public function initialize(){
        parent::initialize();
    }
    public function index($orderid='',$userid='',$sign=''){  
        $order=db('channel_order')->where('orderid',$orderid)->where('userid',$userid)->where('sign',$sign)->find(); 
        if($order['state']===0){
            $order['msg']='订单尚未支付，请核对！';
        }else if($order['state']===1) {
            if(request()->isPost()){ 
                $vurl=url('pay/view/orderview',['orderid'=>$orderid,'appid'=>$order['shopappid'],'sign'=>$sign]);
                if(db('channel_order')->where('orderid',$orderid)->where('userid',$userid)->where('state',1)->update(['state'=>2])!=false){
                    $result = ['code'=>1,'msg'=>'确认成功!','vurl'=>$vurl];
                }else{            
                    $result = ['code'=>0,'msg'=>'该订单已经确认或取消!','vurl'=>$vurl];
                }  
                return $result; 
            }
            $this->assign('order',$order);   
        }else{
            $this->redirect('pay/view/orderview',['orderid'=>$orderid,'appid'=>$order['shopappid'],'sign'=>$sign]);
        }
        return $this->fetch();
    }  

    public function mailcheck($appid='',$orderid='',$userid='',$sign=''){    
        $shang=$this->gopointsauto($orderid,$userid,$sign);
        if(db('channel_order')->where('orderid',$orderid)->where('userid',$userid)->where('state',1)->where('sign',$sign)->update(['state'=>2])!=false){
            $shang=$this->gopointsauto($orderid,$userid,$sign);
            if($shang) $this->redirect('pay/view/orderview',['orderid'=>$orderid,'appid'=>$appid,'sign'=>$sign,'acode'=>$shang['msg']]);
        }else{            
            $this->redirect('pay/view/orderview',['orderid'=>$orderid,'appid'=>$appid,'sign'=>$sign]);
        }   
    }   
    public function orderview($orderid='',$appid='',$sign='',$acode=''){  
        $order=db('channel_order')->where('orderid',$orderid)->where('shopappid',$appid)->where('sign',$sign)->find(); 
        if($order['state']===0){ 
            $this->redirect('pay/api/pay',['orderid'=>$orderid,'appid'=>$order['shopappid']]);
        }else{
            if($order['state']===1){
                $order['msg']='已支付，等待确认【'.$acode.'】';
            }else if($order['state']===2){
                $order['msg']='已支付已确认,交易完成【'.$acode.'】';
            } 
            $this->assign('order',$order);  
            return $this->fetch();
        }    
    }
    public function doit(){  
        if(request()->isPost()){ 
            $orderid=input('orderid');
            $appid=input('appid');
            $sign=input('sign');
            $order=db('channel_order')->where('orderid',$orderid)->where('shopappid',$appid)->where('sign',$sign)->find();
            if($order['state']===0){
                if(db('channel_order')->where('orderid',$orderid)->where('shopappid',$appid)->update(['state'=>1,'paytime'=>time()])!==false){
                    db('channel')->where('id',$order['channelid'])->where('shopappid',$order['shopappid'])->setInc('money_total',$order['money']);   
                    $gotourl= $this->sys['url'].url('pay/view/orderview',['orderid'=>$orderid,'appid'=>$appid,'sign'=>$sign]);
                    $result = ['code'=>1,'msg'=>'支付成功……','callbackurl'=>$gotourl];   
                }else{
                    $result = ['code'=>-1,'msg'=>'系统繁忙，请稍后再试!','orderid'=>$order['orderid']];
                }
            }else{
                $result = ['code'=>-1,'msg'=>'该订单已经支付或取消!','orderid'=>$order['orderid']];
            } 
            if($result['code']===1){
                $this->emailnotice($appid,$order['userid'],$order['orderid'],$order['shopname'],$order['cionname'],$order['number'],$order['money'],$sign,$order['sign'],$order['bankid'],$order['note']);  
            }
            return $result;
        }
    } 
    //自动上分
    protected function gopointsauto($orderid='',$userid='',$sign=''){ 
        $order=db('channel_order')->where('orderid',$orderid)->where('userid',$userid)->where('sign',$sign)->find(); 
        $notifyurl=$this->sys['url'].url('pay/notify/index');
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$notifyurl);//设置抓取的url
        curl_setopt($curl, CURLOPT_HEADER, 'Content-Type:application/x-www-form-urlencoded');//设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_POST, 1);//设置post方式提交
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_POSTFIELDS, "id=".$order['id']);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        $res_data = curl_exec($curl);//执行命令
        curl_close($curl);//关闭URL请求     
    }
    // 邮件通知
    public function emailnotice($appid='',$userid='',$orderid='',$shopname='',$cionname='',$number='',$money='',$sign='',$bankid='',$note=''){ 
        $sender=db('users')->where('id',$userid)->value('email');
        $paytype=db('user_banks')->where('id',$bankid)->value('name');
        $checkurl=$this->sys['url'].url('pay/view/mailcheck',['appid'=>$appid,'orderid'=>$orderid,'userid'=>$userid,'sign'=>$sign]); 
        $content='<div style="width: 90%;max-width: 400px;height: 320px;background: #fff;border-radius: 5px;margin:0 auto;box-shadow: 0 6px 13px 0 rgba(0,0,0,.13);overflow: hidden;"><div style="position: relative;height: 42px;line-height: 42px;padding: 0 15px;border-bottom: 1px solid #f6f6f6;color: #333;border-radius: 2px 2px 0 0;font-size: 14px;">订单信息</div><div style="padding: 10px 15px;line-height: 24px;"><div style="height: 30px;line-height: 30px;font-size: 14px;"><span class="name">订单号：</span><span class="ordernum">'.$orderid.'</span></div><div style="height: 30px;line-height: 30px;font-size: 14px;"><span class="red">提示：请确认该订单是否已收到相应的款项。</span></div><div style="height: 30px;line-height: 30px;font-size: 14px;"><span class="name">支付金：</span> <span class="ordernum">'.$money.'</span><span class="name">元</span></div><div style="height: 30px;line-height: 30px;font-size: 14px;"><span class="name">产品名：</span> <span class="name">'.$cionname.'('.$number.'份)</span></div><div style="height: 30px;line-height: 30px;font-size: 14px;"><span class="name">商户名：</span> <span class="name">'.$shopname.'</span></div><div style="height: 30px;line-height: 30px;font-size: 14px;"><span class="name">备注账号：</span> <span class="name">'.$note.'('.$paytype.')</span></div> <div class="paybot" style="padding-top:40px;"> <a href="'.$checkurl.'" target="_blank" style="color:#fff;  text-decoration: none;display:block;height: 38px;line-height: 38px;padding: 0 18px;background-color: #0cb46a;color: #fff;white-space: nowrap;text-align: center; font-size: 14px;border: none;border-radius: 2px;cursor: pointer;">如果已收到，点击确认订单</a></div></div></div> ';
        $send = send_email($sender, '新订单提醒【'.$orderid.'】', $content);
    } 

    //检查订单状态
    public function checkorder()
    {
        if(request()->isPost()){
            $orderid = input('orderid');
            $appid = input('shopappid');
            $sign = input('sign');
            $order=db('channel_order')->where('orderid',$orderid)->where('shopappid',$appid)->where('sign',$sign)->find();
            // return $result = ['code'=>0,'order'=>$order,'orderid'=>$orderid,'appid'=>$appid,'sign'=>$sign];
            if($order['state']===2){
                $result = ['code'=>1,'msg'=>'支付成功……','callbackurl'=>$order['returnurl']];  
                return $result;
            }elseif($order['state']>2){
                $result = ['code'=>-1,'msg'=>'支付失败，请联系客服','callbackurl'=>$order['returnurl']];
                return $result;
            }
        }
        return True;
    }

}